package com.ksd1.notes.service;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;


import com.ksd1.notes.broadcast.TimeTaskReceiver;
import com.ksd1.notes.util.TimeTool;
import com.ksd1.notes.util.YDTime;

import java.lang.reflect.Field;
import java.util.Calendar;

public class TimeTaskService extends Service {

    AlarmManager alarmManager;
    PendingIntent pIntent;

    private String title;
    private String content;
    private long timestamp;
    private int hour;
    private int minute;

    private TimeTool timeTool;

    public TimeTaskService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        Log.d("20182005085", "onStartCommand");
        //取出传递过来的信息，时分、提示信息
        hour = intent.getIntExtra("hour", 0);
        minute = intent.getIntExtra("minute", 0);
        title = intent.getStringExtra("title");

        timeTool = TimeTool.getInstance();
        content = timeTool.formatTime(hour, minute);//将记时时间进行格式化

        startTimeTask();

        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 开启一个记时任务
     */
    private void startTimeTask() {

        timestamp = YDTime.getInstance().getTimeStamp(hour, minute);//更新时间
        if (timestamp < 0) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                public void run() {
                    Toast.makeText(getApplicationContext(), "时间已过！请重新设置提醒时间！"
                            + System.currentTimeMillis(), Toast.LENGTH_LONG).show();
                }
            });
            return;
        }
        //设置一些必要参数
        timeTool.setTiming(true);//设置目前正在记时
        timeTool.setField("title", title);
        timeTool.setField("content", content);//存到sharedpareferences里面去


        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(), "设置成功！"
                        + content +"时会提醒您", Toast.LENGTH_LONG).show();
            }
        });
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent timeTaskIntent = new Intent(this, TimeTaskReceiver.class);

        pIntent = PendingIntent.getBroadcast(this, 0, timeTaskIntent, PendingIntent.FLAG_CANCEL_CURRENT);


        //设置一个定时任务
        if(android.os.Build.VERSION.SDK_INT >= 19){
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timestamp * 1000, pIntent);
        }else{
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timestamp * 1000, pIntent);
        }
    }

    private void stopTimeTask() {
        alarmManager.cancel(pIntent);//取消定时
        timeTool.setTiming(false);//设置记时状态为取消
    }

    @Override
    public void onDestroy() {
//        Log.d("20182005085", "onDestroy");
        stopTimeTask();
        super.onDestroy();
    }

}
